/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusBinding;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;
import han.bifang.gwt.client.domain.tree.DomainTreeItem;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateOrModifyBusContent2 extends Content {

	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private ServiceBus sc = null;
	private VerticalPanel p = null;
	private String url;
	private ArrayList bindings;
	private TextBox httpUrl, monitorUrl, soapUrl, jmsServerUrl, syncUrl,
			retryUrl,webServicesUrl,logQueryUrl;
	private String parentId;
	private boolean isCreate;
	private boolean urlChanged;

	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			if (isCreate) {
				ArrayList ret = (ArrayList) res.getResponse();
				DomainTreeItem c = new DomainTreeItem();
				c.setId((String) ret.get(0));
				c.setHTML(sc.getName());
				c.setType(EntityConstants.TREE_DOMAIN);
				c.setSubType(EntityConstants.TREE_DOMAIN_BUS);
				c.setKey((String) ret.get(1));
				HashMap m = new HashMap();
				m.put("item", c);
				m.put("domainId", sc.getDomainId());
				listener.onContentChanged(DomainContentConstants.CREATE_BUS2,
						DomainContentConstants.DOMAIN_DETAIL, m);
			} else {
				HashMap m = new HashMap();
				m.put("name", sc.getName());
				listener.onContentChanged(DomainContentConstants.MODIFY_BUS2,
						DomainContentConstants.BUS_DETAIL, m);
			}

		}
	};

	/**
	 * 
	 */
	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			bindings = (ArrayList) res.getResponse();
			for (int i = 0, j = bindings.size(); i < j; i++) {
				BusBinding cb = (BusBinding) bindings.get(i);
				if (cb.getType().equals(EntityConstants.BINDING_HTTP)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("80%");
					p1.setBorderWidth(0);
					HttpBinding hb = (HttpBinding) cb.getBinding();
					HTML h = new HTML();
					TextBox tmp = null;
					if (cb.getName().equals(EntityConstants.BUS_BINDING_HTTP)) {
						h.setHTML(cons.httpBinding());
						httpUrl = new TextBox();
						httpUrl.setVisibleLength(80);
						httpUrl.setText(hb.getUrl());
						tmp = httpUrl;
					}else if (cb.getName().equals(
							EntityConstants.BUS_BINDING_LOG_QUERY)) {
						h.setHTML(cons.logQueryBinding());
						logQueryUrl = new TextBox();
						logQueryUrl.setVisibleLength(80);
						logQueryUrl.setText(hb.getUrl());
						tmp = logQueryUrl;
					}
					else if (cb.getName().equals(
							EntityConstants.BUS_BINDING_MONITOR)) {
						h.setHTML(cons.monitorBinding());
						monitorUrl = new TextBox();
						monitorUrl.setVisibleLength(80);
						monitorUrl.setText(hb.getUrl());
						tmp = monitorUrl;
					} else if (cb.getName().equals(
							EntityConstants.BUS_BINDING_RETRY)) {
						h.setHTML(cons.retryBinding());
						retryUrl = new TextBox();
						retryUrl.setVisibleLength(80);
						retryUrl.setText(hb.getUrl());
						tmp = retryUrl;

					} else if (cb.getName().equals(
							EntityConstants.BUS_BINDING_SYNC)) {
						h.setHTML(cons.syncBinding());
						syncUrl = new TextBox();
						syncUrl.setVisibleLength(80);
						syncUrl.setText(hb.getUrl());
						tmp = syncUrl;
					}
					p1.add(h);
					Grid table = new Grid(3, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.url());

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.http());
					table.setWidget(2, 1, tmp);

					p1.add(table);
					p.add(p1);
				} else if (cb.getType().equals(EntityConstants.BINDING_JMS)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("80%");
					p1.setBorderWidth(0);
					JmsBindingWrapper w = (JmsBindingWrapper) cb.getBinding();
					JmsBinding jb = w.getJmsBinding();
					NormalJms info = (NormalJms)w.getInfo();
					
					Grid table = new Grid(10, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");
					ff.setStyleName(3, 0, "tablecol");
					ff.setStyleName(4, 0, "tablecol");
					ff.setStyleName(5, 0, "tablecol");
					ff.setStyleName(6, 0, "tablecol");
					ff.setStyleName(7, 0, "tablecol");
					ff.setStyleName(8, 0, "tablecol");
					ff.setStyleName(9, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.jmsServer());
					table.setHTML(3, 0, cons.jmsServerVersion());					
					table.setHTML(4, 0, cons.jmsServerUrl());
					table.setHTML(5, 0, cons.jmsFac());
					table.setHTML(6, 0, cons.jmsDestination());
					table.setHTML(7, 0, cons.jmsType());
					table.setHTML(8, 0, cons.jmsUser());
					table.setHTML(9, 0, cons.jmsPass());

					HTML h = new HTML(cons.jmsBinding());
					p1.add(h);

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.jms());

					jmsServerUrl = new TextBox();
					jmsServerUrl.setVisibleLength(80);
					;
					jmsServerUrl.setText(info.getServerUrl());
					table.setWidget(2, 1, jmsServerUrl);
					table.setHTML(3, 1, jb.getServer());
					table.setHTML(4, 1, jb.getServerVersion());
					
					table.setHTML(5, 1, info.getFactoryUrl());
					table.setHTML(6, 1, info.getDestinationJndi());
					table.setHTML(7, 1, cons.jmsTypeQueue());
					//jmsUser = new TextBox();
					//jmsUser.setVisibleLength(32);
					//;
					//jmsUser.setText(jb.getUser());
					table.setHTML(8, 1, "&nbsp;");
					//jmsPass = new PasswordTextBox();
					//jmsPass.setVisibleLength(32);
					
					//jmsPass.setText(jb.getPassword());
					table.setHTML(9, 1, "&nbsp;");
					
					p1.add(table);
					p.add(p1);

				} else if (cb.getType().equals(EntityConstants.BINDING_SOAP)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("80%");
					p1.setBorderWidth(0);
					SoapBinding jb = (SoapBinding) cb
							.getBinding();
					Grid table = new Grid(4, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");
					ff.setStyleName(3, 0, "tablecol");
//					ff.setStyleName(4, 0, "tablecol");
//					ff.setStyleName(5, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.url());
					table.setHTML(3, 0, cons.soapVersion());
//					table.setHTML(4, 0, cons.wsdl());
//					table.setHTML(5, 0, cons.endpoint());

					HTML h = new HTML(cons.soapBinding());
					p1.add(h);

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.soap());
					soapUrl = new TextBox();
					soapUrl.setVisibleLength(80);
					soapUrl.setText(jb.getUrl());
					table.setWidget(2, 1, soapUrl);
					table.setHTML(3, 1, jb.getSoapVersion());
					
//					table.setHTML(3, 1, jb.getNamespace());
//
//					wsdlUrl = new TextBox();
//					wsdlUrl.setVisibleLength(80);
//					;
//					wsdlUrl.setText(jb.getWsdlUrl());
//					table.setWidget(4, 1, wsdlUrl);
//					endpointUrl = new TextBox();
//					endpointUrl.setVisibleLength(80);
//					;
//					endpointUrl.setText(jb.getEndpointUrl());
//					table.setWidget(5, 1, endpointUrl);

					p1.add(table);
					p.add(p1);

				}else if (cb.getType().equals(EntityConstants.BINDING_WEB_SERVICES)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("80%");
					p1.setBorderWidth(0);
					SoapBinding jb = (SoapBinding) cb
							.getBinding();
					Grid table = new Grid(4, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");
					ff.setStyleName(3, 0, "tablecol");
//					ff.setStyleName(4, 0, "tablecol");
//					ff.setStyleName(5, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.url());
					table.setHTML(3, 0, cons.soapVersion());
//					table.setHTML(4, 0, cons.wsdl());
//					table.setHTML(5, 0, cons.endpoint());

					HTML h = new HTML(cons.webservicesBinding());
					p1.add(h);

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.webservices());
					webServicesUrl = new TextBox();
					webServicesUrl.setVisibleLength(80);
					webServicesUrl.setText(jb.getUrl());
					table.setWidget(2, 1, webServicesUrl);
					table.setHTML(3, 1, jb.getSoapVersion());
					
//					table.setHTML(3, 1, jb.getNamespace());
//
//					wsdlUrl = new TextBox();
//					wsdlUrl.setVisibleLength(80);
//					;
//					wsdlUrl.setText(jb.getWsdlUrl());
//					table.setWidget(4, 1, wsdlUrl);
//					endpointUrl = new TextBox();
//					endpointUrl.setVisibleLength(80);
//					;
//					endpointUrl.setText(jb.getEndpointUrl());
//					table.setWidget(5, 1, endpointUrl);

					p1.add(table);
					p.add(p1);

				}


			}
			Button b = null;
			if (isCreate) {
				b = new Button(cons.create());
			} else {
				b = new Button(cons.confirm());
			}
			b.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					if (!validate()) {
						return;
					}
					for (int i = 0, j = bindings.size(); i < j; i++) {
						BusBinding cb = (BusBinding) bindings.get(i);
						if (cb.getName().equals(
								EntityConstants.BUS_BINDING_HTTP)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(httpUrl.getText());
						} else if (cb.getName().equals(
								EntityConstants.BUS_BINDING_MONITOR)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(monitorUrl.getText());
						} else if (cb.getName().equals(
								EntityConstants.BUS_BINDING_RETRY)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(retryUrl.getText());
						}else if (cb.getName().equals(
								EntityConstants.BUS_BINDING_LOG_QUERY)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(logQueryUrl.getText());
						}
						else if (cb.getName().equals(
								EntityConstants.BUS_BINDING_JMS)) {
							JmsBindingWrapper w = (JmsBindingWrapper) cb.getBinding();
							//jb.setPassword(jmsPass.getText());
							//jb.setUser(jmsUser.getText());
							NormalJms info = (NormalJms)w.getInfo();
							info.setServerUrl(jmsServerUrl.getText());

						} else if (cb.getName().equals(
								EntityConstants.BUS_BINDING_SYNC)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(syncUrl.getText());
						} else if (cb.getName().equals(
								EntityConstants.BUS_BINDING_SOAP)) {
							SoapBinding b = (SoapBinding) cb
									.getBinding();
							b.setUrl(soapUrl.getText());

						}else if (cb.getName().equals(
								EntityConstants.BUS_BINDING_WEBSERVICES)) {
							SoapBinding b = (SoapBinding) cb
									.getBinding();
							b.setUrl(webServicesUrl.getText());

						}

					}
					RequestObject req = new RequestObject();
					if (isCreate) {
						req
								.setAction("han.bifang.gwt.server.action.domain.CreateBusAction");
						HashMap map = new HashMap();
						map.put("parentTreeId", parentId);
						map.put("bus", sc);
						map.put("bindings", bindings);
						req.setRequest(map);
					} else {
						req
								.setAction("han.bifang.gwt.server.action.domain.ModifyBusAction");
						HashMap map = new HashMap();

						map.put("bus", sc);
						if (urlChanged) {
							map.put("bindings", bindings);
						}
						req.setRequest(map);
					}
					service.call(req, cb1);

				}

			});
			p.add(b);

		}

	};

	public CreateOrModifyBusContent2(DomainMessages cons,
			BifangGWTServiceAsync service, HashMap params) {
		this.cons = cons;
		this.service = service;
		this.sc = (ServiceBus) params.get("bus");
		this.url = (String) params.get("url");
		this.parentId = (String) params.get("parentTreeId");
		this.isCreate = (Boolean) params.get("create");
		this.urlChanged = params.get("urlChanged") == null ? false
				: (Boolean) params.get("urlChanged");

		p = new VerticalPanel();
		p.setWidth("100%");
		p.setSpacing(20);
		
		HTML hh = new HTML(cons.busBinding());
		p.add(hh);

		initWidget(p);

	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		if (isCreate) {
			req
					.setAction("han.bifang.gwt.server.action.domain.GetBusBindingfromUrlAction");
			ArrayList<String> p = new ArrayList<String>();
			p.add(url);
			p.add(sc.getAppServer());
			p.add(sc.getServerVersion());
			req.setRequest(p);
		} else {
			if (urlChanged) {
				req
						.setAction("han.bifang.gwt.server.action.domain.GetBusBindingfromUrlAction");
				ArrayList<String> p = new ArrayList<String>();
				p.add(url);
				p.add(sc.getAppServer());
				p.add(sc.getServerVersion());
				req.setRequest(p);
			} else {
				req
						.setAction("han.bifang.gwt.server.action.domain.GetBusBindingfromDBAction");
				req.setRequest(sc.getId());
			}
		}
		service.call(req, cb);

	}

	private boolean validate() {
		if (!StringUtils.checkLenght(httpUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.httpUrl(), "1", "1024"));
			return false;
		}
		if (!StringUtils.checkLenght(monitorUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.monitorUrl(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(syncUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.syncUrl(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(jmsServerUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(retryUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.retryUrl(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(soapUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.soapUrl(), "1", "1024"));
			return false;

		}
//		if (!StringUtils.checkLenght(wsdlUrl.getText(), 1, 1024)) {
//			Window.alert(cons.lengthError(cons.wsdl(), "1", "1024"));
//			return false;
//
//		}
		return true;

	}

}
